package com.zhonglz.system.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.zhonglz.commons.base.BaseController;
import com.zhonglz.model.SysLog;
import com.zhonglz.util.result.ResultUtils;
import com.zhonglz.util.tools.DataGrid;
import com.zhonglz.util.tools.ObjectUtils;

/***
 * SysLogController 系统日志管理
 * @author zhonglz
 * @date 2020年7月30日
 */
@Controller
@RequestMapping("/sys/log")
public class LogController extends BaseController{
	/**
     * getUserData  分页查询系统日志列表
     * @param pageNumber 第几页
     * @param limit 每页显示多少条数据
     * @return
     */
	@ResponseBody
    @RequestMapping("selectListByData")
    public DataGrid getUserData(SysLog sysLog,
    		@RequestParam(defaultValue="") String startTime,
    		@RequestParam(defaultValue="") String endTime,
    		@RequestParam(defaultValue = "1") int pageNumber,
			@RequestParam(defaultValue = "10") int pageSize,
			HttpServletRequest req) {
    	DataGrid dataGrid = null;
		try {
			// 封装查询条件参数
			Map<String, Object> mapParam = new HashMap<String, Object>();
			if(!startTime.equals("")&&startTime!=null){
				startTime+=" 00:00:00";
				mapParam.put("startTime",startTime);
			}
			if(!endTime.equals("")&&endTime!=null){
				endTime+=" 23:59:59";
				mapParam.put("endTime",endTime);
			}
			if (ObjectUtils.isNotEmpty(sysLog)) {
				mapParam.put("reqIp",sysLog.getReqIp()); 
				mapParam.put("reqUrl",sysLog.getReqUrl()); 
				mapParam.put("reqParam",sysLog.getReqParam()); 
				mapParam.put("respCode",sysLog.getRespCode()); 
			}
			PageHelper.startPage(pageNumber, pageSize);
			List<SysLog> selectLogList=sysLogService.getLogData(mapParam);
			PageInfo<SysLog> info = new PageInfo<SysLog>(selectLogList);
			// 封装结果集
			dataGrid = new DataGrid(info.getTotal(),selectLogList,info.getPages(),info.getPageNum(),ResultUtils.CODE_SUCCESS);
		} catch (Exception e) {
			e.printStackTrace();
		}
    	return dataGrid;
    }
}
